Skip to content

Conversation

SetTrend
Copy link
Contributor

@SetTrend SetTrend commented Jan 23, 2025

Summary

The current documentation on "How to create a document with namespaces in C# (LINQ to XML)" doesn't explain how to handle the creation of new XAttribute objects that are supposed to have the enclosing XElement's default namespace applied, e.g.:

<root xmlns="default/namespace">
  <element attribute="value"/>
</root>

Without documentation, there are two options that might be hypothetically applicable for creating the correct XML with LINQ to XML in C#:

  1. XNamespace ns = new XNamespace("default/namespace");
    ...
    new XElement(ns + "element", new XAttribute(ns + "attribute", "value"))
  2. XNamespace ns = new XNamespace("default/namespace");
    ...
    new XElement(ns + "element", new XAttribute("attribute", "value"))

This pull request amends one of the examples by including information on how to create XAttribute objects applying to a default namespace.

Fixes #44499
and dotnet/runtime#111703


Internal previews

📄 File 🔗 Preview link
docs/standard/linq/create-document-namespaces-csharp.md How to create a document with namespaces in C# (LINQ to XML)

@SetTrend SetTrend requested a review from a team as a code owner January 23, 2025 20:16
@dotnetrepoman dotnetrepoman bot added this to the January 2025 milestone Jan 23, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Jan 23, 2025
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @BillWagner Do you want to look?

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM as well.
I'll :shipit: now.

@BillWagner BillWagner enabled auto-merge (squash) January 27, 2025 14:16
@BillWagner BillWagner merged commit 677bbc9 into dotnet:main Jan 27, 2025
12 checks passed
@SetTrend SetTrend deleted the XAttributeWithNamespacesExplained branch January 27, 2025 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XAttribute (LINQ to XML): Elaborate on using the default namespace
3 participants